// $Id: handler.h 86 2011-05-04 17:33:43Z cedric.shih@gmail.com $
/*
 * Copyright (C) 2011 Cedric Shih <cedric.shih@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef HANDLER_H_
#define HANDLER_H_

#include <sys/queue.h>
#include <sys/tree.h>

#include "niohttpd/handler.h"

#include "client.h"
#include "daemon.h"

struct niohttpd_handler_item {
	const char *uri;
	TAILQ_ENTRY(niohttpd_handler_item) next;
};

TAILQ_HEAD(niohttpd_handler_list, niohttpd_handler_item);

struct niohttpd_handler_entry {
	char *uri;
	struct niohttpd_handler_item item;
	struct niohttpd_handler_factory factory;
	void *cbarg;
	RB_ENTRY(niohttpd_handler_entry) entry;
};

RB_HEAD(niohttpd_handler_tree, niohttpd_handler_entry);

int niohttpd_handler_entry_compare(
		struct niohttpd_handler_entry *a, struct niohttpd_handler_entry *b);

RB_PROTOTYPE(niohttpd_handler_tree, niohttpd_handler_entry, next,
		niohttpd_handler_entry_compare);

struct niohttpd_handlers {
	struct niohttpd_handler_list list;
	struct niohttpd_handler_tree tree;
};

struct niohttpd_handlers *niohttpd_handlers_new(void);

void niohttpd_handlers_free(struct niohttpd_handlers *handlers);

int niohttpd_handlers_add(struct niohttpd_handlers *handlers,
		const char *uri, struct niohttpd_handler_entry **out);

void niohttpd_handlers_remove(struct niohttpd_handlers *handlers,
		struct niohttpd_handler_entry *entry);

struct niohttpd_handler_entry *niohttpd_handlers_find(
		struct niohttpd_handlers *handlers, const char *uri);

void niohttpd_handlers_clear(struct niohttpd_handlers *handlers);

#endif /* HANDLER_H_ */
